package com.example.day31_video_clip;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.media.MediaPlayer;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.SeekBar;
import android.widget.Toast;
import android.widget.VideoView;

import com.jaygoo.widget.RangeSeekBar;

import java.io.File;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    private VideoView videoView;
    private RangeSeekBar rangeSeekBar;
    private SeekBar musicSeekBar;
    private SeekBar voiceSeekBar;
    private int musicVolume = 0, voiceVolume = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        checkPermission(this);
        initView();
    }
    
    public static boolean checkPermission(Activity activity){
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && activity.checkSelfPermission(
                Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            activity.requestPermissions(new String[]{
                    Manifest.permission.READ_EXTERNAL_STORAGE,
                    Manifest.permission.WRITE_EXTERNAL_STORAGE
            },1);
        }
        return false;
    }

    private void initView() {
        videoView = findViewById(R.id.videoView);
        rangeSeekBar = findViewById(R.id.rangeSeekBar);
        musicSeekBar = findViewById(R.id.musicSeekBar);
        voiceSeekBar = findViewById(R.id.voiceSeekBar);
        musicSeekBar.setMax(100);
        voiceSeekBar.setMax(100);
        musicSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean b) {
                musicVolume=progress;
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
        voiceSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean b) {
                voiceVolume=progress;
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });
    }

    String in1_mp3="music.mp3";
    String in2_mp4="input.mp4";
    Runnable runnable;
    int duration = 0;

    @Override
    protected void onResume() {
        super.onResume();
        final String aacPath = new File(Environment.getExternalStorageDirectory(),in1_mp3).getAbsolutePath();
        final String videoPath = new File(Environment.getExternalStorageDirectory(),in2_mp4).getAbsolutePath();
        try {
            FileUtils.copyAssets(MainActivity.this,in1_mp3,aacPath);            
            FileUtils.copyAssets(MainActivity.this,in2_mp4,videoPath);            
        } catch (Exception e) {
            e.printStackTrace();
        }
        startPlay(new File(Environment.getExternalStorageDirectory(),"input.mp4").getAbsolutePath());
    }
    
    private void startPlay(String path){
        ViewGroup.LayoutParams layoutParams = videoView.getLayoutParams();
        layoutParams.height=675;
        layoutParams.width=1285;
        videoView.setLayoutParams(layoutParams);
        videoView.setVideoPath(path);
        
        videoView.start();
        videoView.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
            @Override
            public void onPrepared(MediaPlayer mediaPlayer) {
                duration = mediaPlayer.getDuration()/1000;
                mediaPlayer.setLooping(true);
                rangeSeekBar.setRange(0,duration);
                rangeSeekBar.setValue(0,duration);
                rangeSeekBar.setEnabled(true);
                rangeSeekBar.requestLayout();
                rangeSeekBar.setOnRangeChangedListener(new RangeSeekBar.OnRangeChangedListener() {
                    @Override
                    public void onRangeChanged(RangeSeekBar view, float min, float max, boolean isFromUser) {
                        videoView.seekTo((int)min*1000);
                    }
                });
                final Handler handler = new Handler();
                runnable = new Runnable() {
                    @Override
                    public void run() {
                        if (videoView.getCurrentPosition() >= rangeSeekBar.getCurrentRange()[1] * 1000) {
                            videoView.seekTo((int) rangeSeekBar.getCurrentRange()[0] * 1000);
                        }
                        handler.postDelayed(runnable,1000);
                    }
                };
                handler.postDelayed(runnable,1000);
            }
        });
    }

    public void startEdit(View view) {
        File cacheDir = Environment.getExternalStorageDirectory();
        final File videoFile = new File(cacheDir,"input.mp4");
        final File audioFile = new File(cacheDir,"music.mp3");
        final File outputFile = new File(cacheDir,"output.mp4");
        new Thread(){
            @Override
            public void run() {
                try {
                    MusicProcess.mixAudioTrack(MainActivity.this,
                    videoFile.getAbsolutePath(),
                    audioFile.getAbsolutePath(),
                    outputFile.getAbsolutePath(),
                    (int)(rangeSeekBar.getCurrentRange()[0]*1000*1000),
                    (int)(rangeSeekBar.getCurrentRange()[1]*1000*1000),
                    voiceVolume,
                    musicVolume);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Log.i("MainActivity", "run===剪辑完毕===");
                        startPlay(new File(Environment.getExternalStorageDirectory(),"output.mp4").getAbsolutePath());
                        Toast.makeText(MainActivity.this, "剪辑完毕", Toast.LENGTH_SHORT).show();
                    }
                });
            }
        }.start();
    }
}
